/**
 * description:webpack配置参数
 * author:
 * date:2020/04/01
 * */
let path = require("path");
let fs = require("fs");
let virtualProjectName = process.env.projectName;

function resolve(dir) {
    return path.resolve(__dirname, '..', dir);
}

let clientViewsFiles = fs.readdirSync(resolve('client/views'));
let proxyUrlArr = clientViewsFiles.some(item => {
    /* 单入口配置proxy */
    return item.indexOf('.') >= 0;
}) ? ['/api'] : clientViewsFiles.map(item => {
        return `/${item}`;
    });

let config = {
    common: {
        htmlDir: resolve("client/index.html"), // 模板html位置
        entryHtmlFileName: 'index.html', //模板html文件名
        faviconDir: resolve('favicon.png'), //favicon文件路径
        virtualDir: process.env.projectName || "", // 项目工程化后加一个虚拟目录
        entryDir: resolve("client/views"), // 项目工程化入口文件夹
        outputRoot: resolve("public"), // 打包后根路径
        entryJsFileName: 'index.js', //入口js文件名
        assetsPublicPath: virtualProjectName ? `${virtualProjectName}/` : '/', // 静态资源引用路径
        assetsOutputDir: "", // 静态资源输出文件夹，相对于打包后根路径
        fromStaticPath: resolve("client/static"), // static文件夹相对于项目根路径地址
        toStaticPath: `static`, // static文件夹复制目标地址，相对于output.path
        dllDir: resolve("dll"), // dll打包路径
        dllJsonSuffix: 'manifest.json', // dll json文件后缀
        dllJsSuffix: 'dll.js', // dll js文件后缀
    },
    development: {
        cssSourceMap: false, // 是否启用source map
        devServer: {
            contentBase: resolve('server/public'),
            publicPath: virtualProjectName ? virtualProjectName : '/', // 静态文件虚拟目录——use()
            host: '0.0.0.0',
            port: '9301',
            hot: true,
            inline: true,
            clientLogLevel: "none",
            open: false,
            historyApiFallback: true,
            disableHostCheck: true,
            compress: true,
            stats: { colors: true },
            proxy: {
                context: proxyUrlArr,
                target: 'http://192.168.8.118:8090',
                pathRewrite: {
                    '^/api': ''
                }
            }
        }
    },
    production: {
        cssSourceMap: false, // 是否启用source map
    },
};
module.exports = Object.assign(config.common, process.env.NODE_ENV === 'development' ? config.development : config.production);
